package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.GetOpenIdBo;
import org.dromara.system.domain.bo.QueryOpenIdByCodeBo;
import org.dromara.system.domain.bo.QueryWxConfigByInvitationCodeBo;
import org.dromara.system.domain.bo.WechatConfigBo;
import org.dromara.system.domain.vo.QueryWxJsapiTicketVo;
import org.dromara.system.domain.vo.WechatConfigVo;
import org.dromara.system.service.IWechatConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信
 *
 * @author Lion Li
 * @date 2025-07-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/wechatConfig")
public class WechatConfigController extends BaseController {

    private final IWechatConfigService wechatConfigService;

    /**
     * 查询微信列表
     */
    @SaCheckPermission("system:config:list")
    @GetMapping("/list")
    public TableDataInfo<WechatConfigVo> list(WechatConfigBo bo, PageQuery pageQuery) {
        return wechatConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出微信列表
     */
    @SaCheckPermission("system:config:export")
    @Log(title = "微信", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WechatConfigBo bo, HttpServletResponse response) {
        List<WechatConfigVo> list = wechatConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信", WechatConfigVo.class, response);
    }

    /**
     * 获取微信详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:config:query")
    @GetMapping("/{id}")
    public R<WechatConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wechatConfigService.queryById(id));
    }

    /**
     * 新增微信
     */
    @SaCheckPermission("system:config:add")
    @Log(title = "微信", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WechatConfigBo bo) {
        return toAjax(wechatConfigService.insertByBo(bo));
    }

    /**
     * 修改微信
     */
    @SaCheckPermission("system:config:edit")
    @Log(title = "微信", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WechatConfigBo bo) {
        return toAjax(wechatConfigService.updateByBo(bo));
    }

    /**
     * 删除微信
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "微信", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wechatConfigService.deleteWithValidByIds(List.of(ids), true));
    }


    //获取微信邀请码
    @GetMapping("/createWechatInvitation")
    public R<String> createWechatInvitation() {
        return R.ok("成功！", wechatConfigService.createWechatInvitation());
    }

    //获取微信邀请码
    @PostMapping("/setOpenId")
    @SaIgnore
    public R<Void> getOpenId(@RequestBody GetOpenIdBo bo) {
        return toAjax(wechatConfigService.getOpenId(bo));
    }

    //获取微信邀请码   公众号
    @PostMapping("/queryOpenIdByCode")
    @SaIgnore
    public R<String> queryOpenIdByCode(@RequestBody QueryOpenIdByCodeBo bo) {
        return R.ok("查询成功！", wechatConfigService.queryOpenIdByCode(bo));
    }

    //获取微信邀请码   小程序
    @PostMapping("/queryOpenIdBySmallProgram")
    @SaIgnore
    public R<String> queryOpenIdBySmallProgram(@RequestBody QueryOpenIdByCodeBo bo) {
        return R.ok("查询成功！", wechatConfigService.queryOpenIdBySmallProgram(bo));
    }


    //根据邀请码获取微信参数配置
    @PostMapping("/queryWxConfigByInvitationCode")
    @SaIgnore
    public R<WechatConfigVo> queryWxConfigByInvitationCode(@RequestBody QueryWxConfigByInvitationCodeBo bo) {
        return R.ok("查询成功！", wechatConfigService.queryWxConfigByInvitationCode(bo));
    }

    //生成JS-SDK权限验证的签名
    @PostMapping("/queryWxJsapiTicket")
    @SaIgnore
    public R<QueryWxJsapiTicketVo> queryWxJsapiTicket(@RequestBody QueryWxConfigByInvitationCodeBo bo) {
        return R.ok("查询成功！", wechatConfigService.queryWxJsapiTicket(bo));
    }
}
